//
//  DingdanzhangtaiViewController.m
//  tianjin
//
//  Created by 鞠超 on 16/8/12.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "DingdanzhangtaiViewController.h"

#import "DingdanzhangtaiTableViewCell.h"
#import "DingdanxiangqingneirongViewController.h"

@interface DingdanzhangtaiViewController () <UITableViewDelegate, UITableViewDataSource>

//tableView
@property (nonatomic, strong) UITableView * tableView;

//头视图
@property (nonatomic, strong) UIView * view_head;

@property (nonatomic, strong) UIButton * btn_1;
@property (nonatomic, strong) UIButton * btn_2;
@property (nonatomic, strong) UIButton * btn_3;
@property (nonatomic, strong) UIButton * btn_4;
@property (nonatomic, strong) UIButton * btn_5;
//
@property (nonatomic, strong) UIView * view_line;


@end

@implementation DingdanzhangtaiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    _topView.backgroundColor = [UIColor blackColor];
    
    _lblTitle.text = @"我的订单";
    _lblTitle.textColor = [UIColor whiteColor];
    _lblTitle.font = [UIFont systemFontOfSize:19];
    
    UIImageView * image_return = [[UIImageView alloc] initWithFrame:CGRectMake(15, 32, 10, 20)];
    image_return.image = [UIImage imageNamed:@"zuofanhui@2x.png"];
    [_topView addSubview:image_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 20, 35, 44);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //头视图
    [self p_headView];
    
    //tableview
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 50, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 50) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:self.tableView];
    //注册
    [self.tableView registerClass:[DingdanzhangtaiTableViewCell class] forCellReuseIdentifier:@"cell_dingdan"];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 2)
    {
        return 50;
    }
    else if(indexPath.row == 3)
    {
        return 65;
    }
    else
    {
        return 100;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    
    if(indexPath.row == 2)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel * number = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 10 - 80 - 150, 15, 150, 20)];
        number.text = @"共3件商品 合计:";
        number.textColor = [UIColor grayColor];
        number.font = [UIFont systemFontOfSize:14];
        number.textAlignment = NSTextAlignmentRight;
        [cell addSubview:number];
        
        UILabel * price = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 10 - 80, 15, 80, 20)];
        price.text = @"￥2000.0";
        price.font = [UIFont systemFontOfSize:16];
        price.textColor = [UIColor redColor];
        [cell addSubview:price];
        
        UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(0, 49, SCREEN_WIDTH, 1)];
        view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cell addSubview:view_line];
    }
    else if(indexPath.row == 3)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, 15)];
        view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cell addSubview:view_line];

        //
        UIButton * btn_3 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_3.frame = CGRectMake(SCREEN_WIDTH - 10 - 80, 7, 80, 36);
        btn_3.layer.cornerRadius = 5;
        btn_3.layer.borderWidth = 1;
        btn_3.tag = indexPath.section + 200;
        btn_3.titleLabel.font = [UIFont systemFontOfSize:16];
        [cell addSubview:btn_3];
        [btn_3 addTarget:self action:@selector(btn3Action:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //
        UIButton * btn_2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_2.frame = CGRectMake(SCREEN_WIDTH - 90 - 10 - 80, 7, 80, 36);
        btn_2.layer.cornerRadius = 5;
        btn_2.layer.borderWidth = 1;
        btn_2.tag = indexPath.section + 500;
        btn_2.titleLabel.font = [UIFont systemFontOfSize:16];
        [cell addSubview:btn_2];
        [btn_2 addTarget:self action:@selector(btn2Action:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //
        UIButton * btn_1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_1.frame = CGRectMake(SCREEN_WIDTH - 180 - 90, 7, 80, 36);
        btn_1.layer.cornerRadius = 5;
        btn_1.layer.borderWidth = 1;
        btn_1.tag = indexPath.section + 800;
        btn_1.titleLabel.font = [UIFont systemFontOfSize:16];
        [cell addSubview:btn_1];
        [btn_1 addTarget:self action:@selector(btn1Action:) forControlEvents:(UIControlEventTouchUpInside)];
        
        
        switch (self.dingdan_type)
        {
            case 0:
            {
                [btn_3 setTitle:@"付款" forState:(UIControlStateNormal)];
                [btn_3 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
                btn_3.layer.borderColor = navi_bar_bg_color.CGColor;
                
                [btn_2 setTitle:@"取消订单" forState:(UIControlStateNormal)];
                [btn_2 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
                btn_2.layer.borderColor = [UIColor grayColor].CGColor;
                
                btn_1.hidden = YES;
            }
                break;
            case 1:
            {
                [btn_3 setTitle:@"取消订单" forState:(UIControlStateNormal)];
                [btn_3 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
                btn_3.layer.borderColor = [UIColor grayColor].CGColor;
                
                btn_2.hidden = YES;
                btn_1.hidden = YES;
            }
                break;
            case 2:
            {
                [btn_3 setTitle:@"投诉" forState:(UIControlStateNormal)];
                [btn_3 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
                btn_3.layer.borderColor = [UIColor grayColor].CGColor;
                
                [btn_2 setTitle:@"确认收货" forState:(UIControlStateNormal)];
                [btn_2 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
                btn_2.layer.borderColor = navi_bar_bg_color.CGColor;
                
                [btn_1 setTitle:@"电话催单" forState:(UIControlStateNormal)];
                [btn_1 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
                btn_1.layer.borderColor = navi_bar_bg_color.CGColor;
            }
                break;
            case 3:
            {
                [btn_3 setTitle:@"删除订单" forState:(UIControlStateNormal)];
                [btn_3 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
                btn_3.layer.borderColor = [UIColor grayColor].CGColor;
                
                [btn_2 setTitle:@"评价" forState:(UIControlStateNormal)];
                [btn_2 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
                btn_2.layer.borderColor = navi_bar_bg_color.CGColor;
                
                btn_1.hidden = YES;
            }
                break;
            case 4:
            {
                [btn_3 setTitle:@"删除订单" forState:(UIControlStateNormal)];
                [btn_3 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
                btn_3.layer.borderColor = [UIColor grayColor].CGColor;
                
                btn_2.hidden = YES;
                btn_1.hidden = YES;
            }
                break;
            default:
                break;
        }
    }
    else
    {
        DingdanzhangtaiTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_dingdan" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        
        return cell;
    }

    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DingdanxiangqingneirongViewController * dingdanxiangqingneirongViewController = [[DingdanxiangqingneirongViewController alloc] init];
    [self showViewController:dingdanxiangqingneirongViewController sender:nil];
}

#pragma mark - 商品下的按钮（1-3个）
- (void )btn3Action:(UIButton *)sender
{
    NSInteger count_x = sender.tag - 200;
    
    NSLog(@"第%ld个订单 ---- 3",(long)count_x);
}

- (void )btn2Action:(UIButton *)sender
{
    NSInteger count_x = sender.tag - 500;
    
    NSLog(@"第%ld个订单 ----- 2",(long)count_x);
}

- (void )btn1Action:(UIButton *)sender
{
    NSInteger count_x = sender.tag - 800;
    
    NSLog(@"第%ld个订单 ------ 1",(long)count_x);
}

#pragma mark - 头视图
- (void )p_headView
{
    self.view_head = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 50)];
    self.view_head.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.view_head];
    
    CGFloat length_x = SCREEN_WIDTH / 5;

    //1
    self.btn_1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_1.frame = CGRectMake(0, 0, length_x, 50);
    self.btn_1.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.btn_1 setTitle:@"待支付" forState:(UIControlStateNormal)];
    [self.btn_1 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.view_head addSubview:self.btn_1];
    
    [self.btn_1 addTarget:self action:@selector(btn_1Action:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //2
    self.btn_2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_2.frame = CGRectMake(length_x, 0, length_x, 50);
    self.btn_2.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.btn_2 setTitle:@"待接单" forState:(UIControlStateNormal)];
    [self.btn_2 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.view_head addSubview:self.btn_2];
    
    [self.btn_2 addTarget:self action:@selector(btn_2Action:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //3
    self.btn_3 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_3.frame = CGRectMake(length_x * 2, 0, length_x, 50);
    self.btn_3.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.btn_3 setTitle:@"配送中" forState:(UIControlStateNormal)];
    [self.btn_3 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.view_head addSubview:self.btn_3];
    
    [self.btn_3 addTarget:self action:@selector(btn_3Action:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //4
    self.btn_4 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_4.frame = CGRectMake(length_x * 3, 0, length_x, 50);
    self.btn_4.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.btn_4 setTitle:@"已完成" forState:(UIControlStateNormal)];
    [self.btn_4 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.view_head addSubview:self.btn_4];
    
    [self.btn_4 addTarget:self action:@selector(btn_4Action:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //5
    self.btn_5 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_5.frame = CGRectMake(length_x * 4, 0, length_x, 50);
    self.btn_5.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.btn_5 setTitle:@"已取消" forState:(UIControlStateNormal)];
    [self.btn_5 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.view_head addSubview:self.btn_5];
    
    [self.btn_5 addTarget:self action:@selector(btn_5Action:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.view_line = [[UIView alloc] initWithFrame:CGRectMake(3, 48, length_x - 6, 2)];
    self.view_line.backgroundColor = navi_bar_bg_color;
    [self.view_head addSubview:self.view_line];
    
    switch (self.dingdan_type)
    {
        case 0:
        {
            [self.btn_1 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
            self.view_line.frame = CGRectMake(3, 48, length_x - 6, 2);
        }
            break;
        case 1:
        {
            [self.btn_2 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
            self.view_line.frame = CGRectMake(length_x + 3, 48, length_x - 6, 2);
        }
            break;
        case 2:
        {
            [self.btn_3 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
            self.view_line.frame = CGRectMake(length_x * 2 + 3, 48, length_x - 6, 2);
        }
            break;
        case 3:
        {
            [self.btn_4 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
            self.view_line.frame = CGRectMake(length_x * 3 + 3, 48, length_x - 6, 2);
        }
            break;
        case 4:
        {
            [self.btn_5 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
            self.view_line.frame = CGRectMake(length_x * 4 + 3, 48, length_x - 6, 2);
        }
            break;
        default:
            break;
    }

}

#pragma mark - 5个btn点击事件
//1
- (void )btn_1Action:(UIButton *)sender
{
    self.dingdan_type = 0;
    
    [self.btn_1 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
    [self.btn_2 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_3 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_4 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_5 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

    [UIView animateWithDuration:0.2 animations:^{
        
        self.view_line.frame = CGRectMake(3 , 48, SCREEN_WIDTH / 5 - 6, 2);
    }];
    
    //
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.tableView reloadData];
    });
}

//2
- (void )btn_2Action:(UIButton *)sender
{
    self.dingdan_type = 1;
    
    [self.btn_1 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_2 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
    [self.btn_3 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_4 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_5 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

    [UIView animateWithDuration:0.2 animations:^{
        
        self.view_line.frame = CGRectMake(SCREEN_WIDTH / 5 + 3, 48, SCREEN_WIDTH / 5 - 6, 2);
    }];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.tableView reloadData];
    });
}

//3
- (void )btn_3Action:(UIButton *)sender
{
    self.dingdan_type = 2;

    [self.btn_1 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_2 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_3 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
    [self.btn_4 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_5 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

    [UIView animateWithDuration:0.2 animations:^{
        
        self.view_line.frame = CGRectMake(SCREEN_WIDTH / 5 * 2 + 3, 48, SCREEN_WIDTH / 5 - 6, 2);
    }];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.tableView reloadData];
    });
}

//4
- (void )btn_4Action:(UIButton *)sender
{
    self.dingdan_type = 3;

    [self.btn_1 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_2 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_3 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_4 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
    [self.btn_5 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

    [UIView animateWithDuration:0.2 animations:^{
        
        self.view_line.frame = CGRectMake(SCREEN_WIDTH / 5 * 3 + 3, 48, SCREEN_WIDTH / 5 - 6, 2);
    }];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.tableView reloadData];
    });
}

//5
- (void )btn_5Action:(UIButton *)sender
{
    self.dingdan_type = 4;

    [self.btn_1 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_2 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_3 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_4 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_5 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.view_line.frame = CGRectMake(SCREEN_WIDTH / 5 * 4 + 3, 48, SCREEN_WIDTH / 5 - 6, 2);
    }];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.tableView reloadData];
    });
}


















@end
